package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginBean
{

	private String _username;
	private String _password;

	public LoginBean()
	{
	}

	public void setUsername(String username_)
	{
		this._username = username_;
	}

	public String getUsername()
	{
		return this._username;
	}

	public void setPassword(String password_)
	{
		this._password = password_;
	}

	public String getPassword()
	{
		return this._password;
	}

	public int validate()
	{
		// TODO Access database and verify the login credentials
		try
		{
			Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
			Statement stmt = conn.createStatement();

			ResultSet results = stmt.executeQuery("SELECT * FROM AdministratorAccounts WHERE Username = '"
							+ this._username
							+ "' && Password = '"
							+ this._password + "';");

			// If there is a match for the username and password
			if (results.next())
			{
				return 1;
			}

			// There wasn't a match, so now check if it was the username or
			// password spelt wrong
			else
			{
				results = stmt.executeQuery("SELECT * FROM AdministratorAccounts WHERE Username = '"
								+ this._username + "';");

				if (results.next())
				{
					// Password is input incorrectly
					return 2;
				}
				else
				{
					// No username exists
					return 3;
				}
			}
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}

		// Error code
		return 0;
	}

	public static void main(String[] args)
	{
		System.out.println("LoginBean.");
	}
}